package mysql

import (
	"gorm.io/gorm"
	"work/workrpc/workrpc"
)

type Story struct {
	gorm.Model
	Title  string `gorm:"type:varchar(30);comment:标题"`
	Typer  string `gorm:"type:varchar(30);comment:小说类型"`
	Label  string `gorm:"type:varchar(30);comment:小说标签"`
	Brief  string `gorm:"type:varchar(30);comment:小说简介"`
	UserId int64  `gorm:"type:int(11);comment:用户ID"`
}

func (s *Story) StoryAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

func (s *Story) FindStory(db *gorm.DB, id int64) error {
	return db.Debug().Where("stories.id=?", id).Find(s).Error
}
func StoryUserList(db *gorm.DB, in *workrpc.StoryAndUserListRequest) ([]*workrpc.StoryAndUserList, error) {
	var list []*workrpc.StoryAndUserList
	//SELECT
	//  title,
	//  typer,
	//  label,
	//  brief,
	//  nick_name,
	//  head_image
	//FROM
	//  stories
	//  LEFT JOIN users ON stories.user_id = users.id
	db.Debug().Model(&Story{}).Select(
		"users.nick_name",
		"users.head_image",
		"stories.title",
		"stories.typer",
		"stories.label",
		"stories.brief",
	).Joins("LEFT JOIN users ON stories.user_id = users.id").Scopes(Paginate(int(in.Page), int(in.Size))).
		Where("title like? or typer like?", "%"+in.KeyWord+"%", "%"+in.KeyWord+"%").
		Order("stories.created_at desc").
		Find(&list)
	return list, err
}
